import { FormCreateType } from '@/types/form-create';
import { isArray } from 'lodash-es';

type GetQueryParams = {
	data: Record<string, any>;
	fieldTypes: { key: string; type: FormCreateType }[];
};
export const getQuery = ({ data, fieldTypes }: GetQueryParams) => {
	const queryList = Object.entries(data)
		.map(([key, value]) => {
			const fieldType = fieldTypes.find((item) => item.key === key);
			if (fieldType) {
				switch (fieldType.type) {
					case 'n-input':
						return `/${value}/.test(${key})`;
					case 'n-select':
					case 'data-select':
						if (isArray(value)) {
							return `${key} in ['${value.join("','")}']`;
						} else {
							return `${value} == ${key}`;
						}
					case 'n-date-picker':
						return `${key} >= ${value[0]} && ${key} <= ${value[1]}`;
					case 'n-input-number':
						return `${value} == ${key}`;
					case 'n-switch':
						return `${key} == ${value}`;
					case 'n-cascader':
						return `${key} == ${value}`;
					default:
						return '';
				}
			}
		})
		.filter(Boolean);

	return queryList.join(' && ');
};

type GetPaginationParams = GetQueryParams & PageParams;
export const getPaginationParams = ({ data, fieldTypes, page, pageSize }: GetPaginationParams) => {
	return {
		query: getQuery({ data, fieldTypes }),
		limit: pageSize,
		skip: (page - 1) * pageSize,
	};
};
